/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;
/**
 * 说明：
 * 添加物品领用Model
 * @Author：
 * @Date：2012-03-21
 */
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.bean.TBillDetailDes;
import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.model.billDetail.BillDetailModelIf;

public class OtherReserveAddModel extends AbstractTableModel implements BillDetailModelIf {

	private static final long serialVersionUID = 1L;
	private List<TBillDetail> tBillDetailList=new ArrayList<TBillDetail>();
	private Vector<String> columns = new Vector<String>();
	private boolean editable = true;
	public OtherReserveAddModel(){
		columns.add("");
		columns.add("详细编号");
		columns.add("商品编号");
		columns.add("商品名称");
		columns.add("商品数量");
		columns.add("商品加权价");
		columns.add("出库原因");
		columns.add("合计");
	}
	
	//获得行数
	public int getRowCount(){
		return tBillDetailList.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		TBillDetail tBillDetail = tBillDetailList.get(row);
		if(tBillDetail!=null){
			switch (column) {
				case 0:return tBillDetail.isFlag();
				case 1:
					return tBillDetail.getDetailID();
				case 2:
					return tBillDetail.getCommodityID();
				case 3:
					if (tBillDetail.getCommodity() != null && tBillDetail.getCommodity().getName() != null)
						return tBillDetail.getCommodity().getName();
					else 
						return "";
				case 4:
					return tBillDetail.getNum();
				case 5:
					return tBillDetail.getPrice();
				case 6:
					if (tBillDetail.getTBillDetailDes() != null && tBillDetail.getTBillDetailDes().size()!= 0){
						return ((TBillDetailDes)(tBillDetail.getTBillDetailDes().toArray()[0])).getDes();
					}
					return "";
				case 7:
					return tBillDetail.getNum()*tBillDetail.getPrice();
			}
		}
		return null;
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	

	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		if (columnIndex == 1 || columnIndex == 3 || columnIndex == 5 || columnIndex == 7) {
			return false;
		}
		return editable;
	}	//设置单元格是否可编辑
	public void setEditable(boolean editable) {
		this.editable  = editable;
		
	}
	public void removeData(int row){
		tBillDetailList.remove(row);
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (columnIndex == 0)
			return Boolean.class;
		return super.getColumnClass(columnIndex);
	}

	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		TBillDetail tBillDetail = tBillDetailList.get(rowIndex);
		switch (columnIndex) {
			case 0:
			if(aValue!=null && aValue instanceof Boolean){
				tBillDetail.setFlag(Boolean.parseBoolean(aValue.toString()));
			}
			return ;
			case 1:
				return;
			case 2:
				if(aValue!=null){
					if(aValue instanceof TCommodity){
						TCommodity comm=(TCommodity)aValue;
						tBillDetail.setCommodityID(comm.getCommodityID());
//						tBillDetail.setPrice(ApplicationContext.WeightingPrice(comm));
						tBillDetail.setPrice(comm.getAvgPrice());
						tBillDetail.setCommodity(comm);
						fireTableRowsUpdated(rowIndex, rowIndex);
					}
				}
				return;
			case 3:
				return;
			case 4:
				if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherNumberScheme(aValue.toString())&&aValue.toString().length()<10){
					tBillDetail.setNum(Integer.parseInt(aValue.toString()));
				}
				return;
			case 5:
				if(aValue!=null){
					if(aValue instanceof TCommodity){
						TCommodity comm=(TCommodity)aValue;
						tBillDetail.setCommodityID(comm.getCommodityID());
//						tBillDetail.setPrice(ApplicationContext.WeightingPrice(comm));
						tBillDetail.setPrice(comm.getAvgPrice());
						tBillDetail.setCommodity(comm);
						fireTableRowsUpdated(rowIndex, rowIndex);
					}
				}
				return;
			case 6:
				if(aValue != null && !aValue.toString().equals("")) {
					Set<TBillDetailDes> TBillDetailDess=new HashSet<TBillDetailDes>(0);
					TBillDetailDes tBillDetailDes = new TBillDetailDes();
					tBillDetailDes.setDes(aValue.toString());
					tBillDetailDes.setTBillDetail(tBillDetail);
					TBillDetailDess.add(tBillDetailDes);
					tBillDetail.setTBillDetailDes(TBillDetailDess);
				}
			case 7:
				return;
		}
		super.setValueAt(aValue, rowIndex, columnIndex);
	}
	/**
	 * 添加
	 * */
	@Override
	public void addBillDetail(TBillDetail tBillDetail) {
		tBillDetailList.add(tBillDetail);
		fireTableRowsInserted(tBillDetailList.size()-1, tBillDetailList.size()-1);
		
	}
	/**
	 * 删除
	 * */
	@Override
	public void delBillDetail(int index) {
		if (tBillDetailList!=null&&tBillDetailList.size() > 0) {
			if (index >= 0 && index < tBillDetailList.size()) {
				tBillDetailList.remove(index);
				fireTableRowsInserted(index, index);
				return;
			}
			tBillDetailList.remove(tBillDetailList.size()-1);
			fireTableRowsInserted(tBillDetailList.size()-1, tBillDetailList.size()-1);
		}
	}
	/**
	 * 修改
	 * */
	@Override
	public void modifyBillDetail(int index,TBillDetail tBillDetail) {
		if (tBillDetailList!=null&&tBillDetailList.size() > 0) {
			if (index >= 0 && index < tBillDetailList.size()) {
				tBillDetailList.set(index, tBillDetail);
				fireTableRowsInserted(index, index);
			}
		}
	}
	/**
	 * 返回所有
	 * */
	@Override
	public List<TBillDetail> findAllBillDetailList() {
		return tBillDetailList;
	}
	/**
	 * 设置当前list数据
	 * */
	@Override
	public void setBillDetailList(List<TBillDetail> billDetailList){
		this.tBillDetailList=billDetailList;
	}
	/**
	 * 清空所有
	 * */
	@Override
	public void clearAllBillDetail(){
		tBillDetailList.clear();
	}
}
